package org.yourscrum.domain;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Jettro Coenradie
 */
@Entity
@Table(name = "task")
public class Task extends AbstractEntity {
    @ManyToOne
    @JoinColumn(name = "story_id")
    private Story story;

    @Column(name = "description")
    private String description;

    @Column(name = "estimated_hours")
    private int estimatedHours = 0;

    @Column(name = "actual_hours")
    private int actualHours = 0;

    @OneToMany(mappedBy = "task")
    private List<WorkLog> workDone = new ArrayList<WorkLog>();

    public Task() {
    }

    public Task(Story story, String description) {
        this.story = story;
        this.description = description;
    }

    public void changeDescription(String description) {
        this.description = description;
    }

    public void changeEstimatedHours(int estimatedHours) {
        this.estimatedHours = estimatedHours;
    }

    public synchronized void logWorkDone(int actualHours, String descriptionOfWork) {
        this.actualHours += actualHours;
        workDone.add(new WorkLog(this,descriptionOfWork));
    }


    /* Getter */
    public Story getStory() {
        return story;
    }

    public String getDescription() {
        return description;
    }

    public int getEstimatedHours() {
        return estimatedHours;
    }

    public int getActualHours() {
        return actualHours;
    }

    public List<WorkLog> getWorkDone() {
        return Collections.unmodifiableList(workDone);
    }
}
